﻿#include "Sort.h"
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include<stdbool.h>
#include<stddef.h>//NULL
#include<time.h>
//交换函数
void Swap(int* a, int* b)
{
	int p = *a;
	*a = *b;
	*b = p;
}
//直接插入排序(升序)
void InsertSort1(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		//我们把需要比较的数放入tmp中，然后先与第i个数据比较，依次往前进行比较插入
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (arr[end] > tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		//如果end=-1我们不能进行数组越界，所以要进行+1操作
		arr[end + 1] = tmp;
	}
}
//希尔排序
void ShellSort1(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (arr[end] > tmp)
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}
//直接选择排序
void SelectSort1(int* arr, int n)
{
	int begin = 0, end = n - 1;
	while (begin < end)
	{
		int max, min;
		max = min = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			//我们从开始位置后面的数据进行遍历，原因不用解释
			if (arr[i] < arr[min])
			{
				min = i;
			}
			if (arr[i] > arr[max])
			{
				max = i;
			}
		}
		if (max == begin)
		{
			max = min;
		}
		Swap(&arr[min], &arr[begin]);
		Swap(&arr[max], &arr[end]);
		begin++;
		end--;
	}
}//堆的结构
//能存储的数据有很多种

typedef int HPDataType;
typedef struct Heap
{
	HPDataType* arr;
	int size;//有效数据的个数
	int capacity;//容量
}HP;
//堆的初始化
void HPInit(HP* php)
{
	assert(php);
	php->arr = NULL;
	php->size = php->capacity = 0;
}
//堆的销毁
void HPDesTory(HP* php)
{
	//判断是否为NULL
	if (php->arr)
	{
		free(php->arr);
	}
	php->arr = NULL;
	php->size = php->capacity = 0;
}
//交换函数
//void Swap(HPDataType* x, HPDataType* y)
//{
//	HPDataType tmp = *x;
//	*x = *y;
//	*y = tmp;
//}
////向上调整算法
//void AdjustUp(HPDataType* arr, int child)
//{
//    HPDataType parent = (child - 1) / 2;
//    while (child > 0)
//    {
//        if (arr[child] < arr[parent])
//        {
//            Swap(&arr[child], &arr[parent]);
//            //或者可以加个continue,之后把else去掉，只留break；
//            child = parent;
//            parent = (child - 1) / 2;
//        }
//        else
//        {
//            break;
//        }
//    }
//}
//向上调整算法
void AdjustUp(HPDataType* arr, int child)
{
	HPDataType parent = (child - 1) / 2;
	while (child > 0)
	{
		if (arr[child] > arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			//或者可以加个continue,之后把else去掉，只留break；
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
//插入数据
void HPPush(HP* php, HPDataType x)
{
	assert(php);
	if (php->size == php->capacity)
	{
		int newcapacity = php->capacity == 0 ? 4 : 2 * php->capacity;
		//增容
		HPDataType* tmp = (HPDataType*)realloc(php->arr, sizeof(HPDataType) * (newcapacity));
		//增容失败
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		php->arr = tmp;
		php->capacity = newcapacity;
	}
	php->arr[php->size] = x;
	//第二个传的参数是php->size，因为下标这个时候最大已经是size,有size+1个数据，我们还没有执行++的操作
	AdjustUp(php->arr, php->size);
	++php->size;
}
//判空
bool HPEmpty(HP* php)
{
	assert(php);
	return php->size == 0;
}
//向下调整算法
void AdjustDown(HPDataType* arr, int parent, int n)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		//先找最小的
		if (child + 1 < n && arr[child] > arr[child + 1])
		{
			child++;
		}
		if (arr[parent] > arr[child])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
////向下调整算法建堆
//void HeapSort(HPDataType* arr, int n)
//{
//    for (int i = (n - 2) / 2; i >= 0; i--)
//    {
//        AdjustDown(arr, i, n);
//    }
//    //堆排序
//    int end = n - 1;
//    while (end > 0)
//    {
//        Swap(&arr[0], &arr[end]);
//        AdjustDown(arr, 0, end);
//        end--;
//    }
//}
//删除栈顶数据
void HPpop(HP* php)
{
	assert(!HPEmpty(php));
	Swap(&php->arr[0], &php->arr[php->size - 1]);
	--php->size;
	AdjustDown(php->arr, 0, php->size);
}
//取堆顶元素
HPDataType HPTop(HP* php)
{
	assert(!HPEmpty(php));
	return php->arr[0];
}
//测试函数
void test()
{
	HP hp;
	HPInit(&hp);
	HPPush(&hp, 4);
	HPPush(&hp, 6);
	HPPush(&hp, 8);
	HPPush(&hp, 10);
	for (int i = 0; i < 4; i++)
	{
		printf("%d ", hp.arr[i]);
	}
	while (!HPEmpty(&hp))
	{
		printf("%d ", HPTop(&hp));
		HPpop(&hp);
	}
	HPDesTory(&hp);
}
//向下调整算法建堆
void HeapSort(HPDataType* arr, int n)
{
	for (int i = (n - 2) / 2; i >= 0; i--)
	{
		AdjustDown(arr, i, n);
	}
	//堆排序
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, end);
		end--;
	}
}
//向上调整算法建堆
void HeapSort01(HPDataType* arr, int n)
{
	for (int i = 0; i < n; i++)
	{
		AdjustUp(arr, i);
	}
	//堆排序
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, end);
		end--;
	}
}
//int main()
//{
//    //test();
//    int arr[] = { 17,20,10,13,19,15 };
//    int n = sizeof(arr) / sizeof(arr[0]);
//    HeapSort01(arr, n);
//    for (int i = 0; i < n; i++)
//    {
//        printf("%d ", arr[i]);
//    }
//    return 0;
//}
//向下调整算法
//void AdjustDown(HPDataType* arr, int parent, int n)
//{
//    int child = parent * 2 + 1;
//    while (child < n)
//    {
//        //先找最小的
//        if (child + 1 < n && arr[child] > arr[child + 1])
//        {
//            child++;
//        }
//        if (arr[parent] > arr[child])
//        {
//            Swap(&arr[child], &arr[parent]);
//            parent = child;
//            child = parent * 2 + 1;
//        }
//        else
//        {
//            break;
//        }
//    }
//}
void CreateNDate()
{
	//造数据
	long n = 100000;
	//生成随机数
	srand(time(0));
	const char* file = "data.txt";
	FILE* fin = fopen(file, "w");
	if (fin == NULL)
	{
		perror("fopen error!\n");
		return;
	}
	for (long i = 0; i < n; i++)
	{
		long x = (rand() + i) % 1000000;
		fprintf(fin, "%d\n", x);
	}
	fclose(fin);
}
void TopK()
{
	int k = 0;
	printf("请输入K: ");
	scanf("%d", &k);
	//读取文件前K个数据建堆
	const char* file = "data.txt";
	FILE* fout = fopen(file, "r");
	if (fout == NULL)
	{
		perror("fopen error!\n");
		exit(1);
	}
	//找最大的前K个数，建小堆
	int* minHeap = (int*)malloc(sizeof(int) * k);
	if (minHeap == NULL)
	{
		perror("malloc fail!\n");
		exit(2);
	}
	//读取文件前K个数据建堆
	for (int i = 0; i < k; i++)
	{
		fscanf(fout, "%d", &minHeap[i]);
	}
	//建堆
	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(minHeap, i, k);
	}
	//变量剩下的n-k个数据，进行调整入堆
	long x;
	while (fscanf(fout, "%d", &x) != EOF)
	{
		if (x > minHeap[0])
		{
			minHeap[0] = x;
		}
		AdjustDown(minHeap, 0, k);
	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ", minHeap[i]);
	}
	fclose(fout);
	free(minHeap);
	minHeap = NULL;
}
//int main()
//{
//	CreateNDate();
//	TopK();
//	return 0;
//}
//冒泡排序
void BubbleSort(int* arr, int n)
{
	for (int i = 0; i < n-1; i++)
	{
		int exchange = 0;
		for (int j = 0; j < n - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				exchange = 1;
				Swap(&arr[j], &arr[j + 1]);
			}
		}
		if (exchange == 0)
		{
			break;
		}
	}
}
// 测试排序的性能对⽐
void TestOP()
{
	srand(time(0));
	const int N = 100000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	//int* a5 = (int*)malloc(sizeof(int) * N);
	//int* a6 = (int*)malloc(sizeof(int) * N);
	int* a7 = (int*)malloc(sizeof(int) * N);
	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		//a5[i] = a1[i];
		//a6[i] = a1[i];
		a7[i] = a1[i];
	}
	int begin1 = clock();
	InsertSort1(a1, N);
	int end1 = clock();
	int begin2 = clock();
	ShellSort1(a2, N);
	int end2 = clock();
	int begin3 = clock();
	SelectSort1(a3, N);
	int end3 = clock();
	int begin4 = clock();
	HeapSort01(a4, N);
	int end4 = clock();
	int begin5 = clock();
	//QuickSort(a5, 0, N - 1);
	//int end5 = clock();
	//int begin6 = clock();
	//MergeSort(a6, N);
	//int end6 = clock();
	int begin7 = clock();
	BubbleSort(a7, N);
	int end7 = clock();
	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("SelectSort:%d\n", end3 - begin3);
	printf("HeapSort:%d\n", end4 - begin4);
	//printf("QuickSort:%d\n", end5 - begin5);
	//printf("MergeSort:%d\n", end6 - begin6);
	printf("BubbleSort:%d\n", end7 - begin7);
	free(a1);
	free(a2);
	free(a3);
	free(a4);
	//free(a5);
	//free(a6);

	free(a7);
}
int main()
{
	int a[] = { 1,2,3,4,5,6,7,8,9 };
	//int a[] = { 9,8,4,2,4,1,3,7,5 };
	//int a[] = { 2,3,4,3,4,2,7,9,2,4 };
	//int a[] = { 100,101,195,124,125,129 };
	int n = sizeof(a) / sizeof(a[0]);
	printf("排序之前： ");
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	BubbleSort(a, n);
	printf("\n排序之后： ");
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}